package cn.kooki.app.duobao.ui.Activity.User;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.socks.library.KLog;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseActivity;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.User.LoginResponse;
import cn.kooki.app.duobao.data.Bean.User.LoginType;
import cn.kooki.app.duobao.data.Bean.User.UserInfo;
import cn.kooki.app.duobao.data.bus.LoginChangeEvent;
import cn.kooki.app.duobao.data.bus.UserRefrshEvent;
import cn.kooki.app.duobao.data.bus.WinEvent;
import cn.kooki.app.duobao.data.constant.Constants;
import cn.kooki.app.duobao.ui.Activity.Comm.AuthVerifyActivity;
import cn.kooki.app.duobao.util.DESUtils;
import cn.kooki.app.duobao.util.SharePreferencesUtils;
import cn.kooki.app.duobao.util.manager.LoginManager;
import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2015/12/19.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener, LoginManager.LoginHandler {

    @Bind(R.id.top_back)
    ImageButton topBack;
    @Bind(R.id.top_text_center)
    TextView topTextCenter;
    @Bind(R.id.top_refresh)
    ImageButton topRefresh;
    @Bind(R.id.top_cart)
    ImageButton topCart;
    @Bind(R.id.action_edit)
    TextView actionEdit;
    @Bind(R.id.toolbar)
    RelativeLayout toolbar;
    @Bind(R.id.registered_tips)
    TextView registeredTips;
    @Bind(R.id.registered_email)
    TextView registeredEmail;
    @Bind(R.id.registered_normal)
    TextView registeredNormal;
    @Bind(R.id.email_icon)
    ImageView emailIcon;
    @Bind(R.id.email)
    AutoCompleteTextView email;
    @Bind(R.id.email_wrapper)
    RelativeLayout emailWrapper;
    @Bind(R.id.pwd_icon)
    ImageView pwdIcon;
    @Bind(R.id.password)
    EditText password;
    @Bind(R.id.login_forget)
    TextView loginForget;
    @Bind(R.id.pass_wrapper)
    RelativeLayout passWrapper;
    @Bind(R.id.login)
    Button login;
    @Bind(R.id.login_reg)
    Button loginReg;
    @Bind(R.id.imageView)
    ImageView imageView;
    @Bind(R.id.more_auth_method)
    TextView moreAuthMethod;
    @Bind(R.id.auth_weixin_image)
    ImageButton authWeixinImage;
    @Bind(R.id.auth_weixin_text)
    TextView authWeixinText;
    @Bind(R.id.auth_qq_image)
    ImageButton authQqImage;
    @Bind(R.id.auth_qq_text)
    TextView authQqText;
    @Bind(R.id.auth_sina_image)
    ImageButton authSinaImage;
    @Bind(R.id.auth_sina_text)
    TextView authSinaText;
    @Bind(R.id.login_normal)
    LinearLayout loginNormal;
    @Bind(R.id.login_layout)
    LinearLayout loginLayout;
    @Bind(R.id.layout_otherLogin_tips)
    FrameLayout layoutOtherLoginTips;
    private UserInfo userInfo;
    private UMShareAPI mShareAPI;
    private LoginManager manager;

    @Override
    protected void setToolbar() {
        topBack.setImageResource(R.drawable.ic_top_back);
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topTextCenter.setText(R.string.title_activity_login);
        topTextCenter.setVisibility(View.VISIBLE);
        topBack.setVisibility(View.VISIBLE);
    }

    @Override
    protected void setLayoutContentView() {
        setContentView(R.layout.fragment_login);
        ButterKnife.bind(this);

    }

    @Override
    protected void afterView() {


        login.setOnClickListener(this);
        authSinaImage.setOnClickListener(this);
        authWeixinImage.setOnClickListener(this);
        authQqImage.setOnClickListener(this);
        loginReg.setOnClickListener(this);
        hideNotsupport();

    }

    private void hideNotsupport() {
        int type = SharePreferencesUtils.getInt(this, LoginType.LOING);
        authWeixinImage.setVisibility(type == 1 ? View.VISIBLE:View.GONE);
        authWeixinText.setVisibility(type == 1? View.VISIBLE:View.GONE);

        authQqImage.setVisibility(type == 1 ? View.VISIBLE:View.GONE);
        authQqText.setVisibility(type == 1? View.VISIBLE:View.GONE);
        layoutOtherLoginTips.setVisibility(type == 1? View.VISIBLE:View.GONE);

    }

    @Override
    public void onClick(View v) {
        if (v == login) {
            if (checkForm()) {
                dologin();
            }
        }

        if (v == authWeixinImage) {
            dologinOther(0);
        }

        if (v == loginReg) {
            intent(RegActivity.class);
        }

        if (v == authSinaImage) {
            dologinOther(2);
        }

        if (v == authQqImage) {
            dologinOther(1);
        }
    }

    private void dologinOther(int type) {
        intent(AuthVerifyActivity.class, "type", type);
    }


    private void dologin() {
        String md5 = DESUtils.MD5(password.getText().toString());
        manager = new LoginManager();
        manager.doLogin(this, email.getText().toString(), md5, this);
    }


    private boolean checkForm() {
        hideSoftKeyBoard();
        if (TextUtils.isEmpty(email.getText().toString())) {
            showToastMessage(getResources().getString(R.string.moblie_is_empty));
            return false;
        }
        if (TextUtils.isEmpty(password.getText().toString()) || password.getText().length() < 6) {
            return false;
        }

        return true;
    }

    @Override
    public void onLoginStart() {
        showLoading(getString(R.string.login_loading));
    }

    @Override
    public void onLoginComplete() {
        dismissLoading();
        showToastMessage("登录成功");
        email.setText("");
        password.setText("");
        LoginChangeEvent changeEvent = new LoginChangeEvent();
        changeEvent.islogin = true;
        EventBus.getDefault().post(changeEvent);

    }


    public void onEventMainThread(LoginChangeEvent event) {
        finish();
    }

    @Override
    public void onLoginFair(String msg) {
        dismissLoading();
        showToastMessage(msg);
    }

    @Override
    public void onOtherLoginComplete() {
        LoginChangeEvent changeEvent = new LoginChangeEvent();
        changeEvent.islogin = true;
        EventBus.getDefault().post(changeEvent);
    }
}
